package com.jg.visitor.common.utils;

import com.jg.visitor.common.bean.AppConfigs;
import com.jg.visitor.controller.enums.ProcessEnum;
import com.jg.visitor.controller.vo.WxTemplateMsgVO;
import com.jg.visitor.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class WechatUtils {

    @Resource
    private UserService userService;
    @Resource
    private AppConfigs appConfigs;

    /**
     * 推送微信模板消息
     * @param applyOrderNum 申请单号
     * @param customerName 客户名称
     * @param processEnum 进度
     */
    @Async
    public void sendTemplateMsgToApprover(String applyOrderNum,
                                          String customerName,
                                          String openId,
                                          ProcessEnum processEnum){
        String globalAccessToken = userService.getGlobalAccessToken();

        if(StringUtils.isEmpty(globalAccessToken)){
            return;
        }

        WxTemplateMsgVO msgVO = new WxTemplateMsgVO();
        msgVO.setAccess_token(userService.getGlobalAccessToken());
        msgVO.setTemplate_id(appConfigs.getTemplateId());
        msgVO.setTouser(openId);
        msgVO.setLang(appConfigs.getLanguage());
        msgVO.setPage(appConfigs.getPage());
        msgVO.setMiniprogram_state(appConfigs.getMiniprogramState());

        WxTemplateMsgVO.Data data = new WxTemplateMsgVO.Data();

        WxTemplateMsgVO.Value orderNumValue = new WxTemplateMsgVO.Value();
        orderNumValue.setValue(applyOrderNum);

        WxTemplateMsgVO.Value customerNameValue = new WxTemplateMsgVO.Value();
        customerNameValue.setValue(customerName);

        WxTemplateMsgVO.Value processEnumValue = new WxTemplateMsgVO.Value();
        processEnumValue.setValue(processEnum.getProcessName());

        data.setCharacter_string1(orderNumValue);
        data.setPhrase4(processEnumValue);
        data.setThing9(customerNameValue);


        msgVO.setData(data);

        userService.sendWechatTemplateMsg(msgVO);
    }
}
